package com.ruoyi.business.service.impl;

import java.io.FileNotFoundException;
import java.util.List;

import com.ruoyi.business.domain.DefinitionInfo;
import com.ruoyi.business.service.IProcessService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.DefinitionDetailMapper;
import com.ruoyi.business.domain.DefinitionDetail;
import com.ruoyi.business.service.IDefinitionDetailService;
import com.ruoyi.common.core.text.Convert;

/**
 * 流程定义明细Service业务层处理
 * 
 * @author wolfcode
 * @date 2021-07-19
 */
@Service
public class DefinitionDetailServiceImpl implements IDefinitionDetailService 
{
    @Autowired
    private DefinitionDetailMapper definitionDetailMapper;
     @Autowired
     private IProcessService processService;

    /**
     * 查询流程定义明细
     * 
     * @param definitionInfo 流程定义明细ID
     * @return 流程定义明细
     */
    @Override
    public DefinitionDetail selectDefinitionDetailById(Long definitionInfo)
    {
        return definitionDetailMapper.selectDefinitionDetailById(definitionInfo);
    }

    /**
     * 查询流程定义明细列表
     * 
     * @param definitionDetail 流程定义明细
     * @return 流程定义明细
     */
    @Override
    public List<DefinitionDetail> selectDefinitionDetailList(DefinitionDetail definitionDetail)
    {
         List<DefinitionDetail> definitionDetails = definitionDetailMapper.selectDefinitionDetailList(definitionDetail);
         for (DefinitionDetail detail : definitionDetails) {
              //根据部署ID获取流程定义对象
              ProcessDefinition processDefinition = processService.getProcessDefinitionByDeploymentId(detail.getDeploymentId());
              detail.setDeployKey(processDefinition.getKey());//部署key
              detail.setVersion(processDefinition.getVersion());//部署version
              detail.setDescription(processDefinition.getDescription());//部署描述信息



         }
         return definitionDetails;
    }

    /**
     * 新增流程定义明细
     *
     * @param definitionDetail 流程定义明细
     * @return 结果
     */
    @Override
    public int insertDefinitionDetail(DefinitionDetail definitionDetail)
    {
        return definitionDetailMapper.insertDefinitionDetail(definitionDetail);
    }

    /**
     * 修改流程定义明细
     *
     * @param definitionDetail 流程定义明细
     * @return 结果
     */
    @Override
    public int updateDefinitionDetail(DefinitionDetail definitionDetail)
    {
        return definitionDetailMapper.updateDefinitionDetail(definitionDetail);
    }

    /**
     * 删除流程定义明细对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDefinitionDetailByIds(String ids)
    {
        return definitionDetailMapper.deleteDefinitionDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除流程定义明细信息
     * 
     * @param definitionInfo 流程定义明细ID
     * @return 结果
     */
    @Override
    public int deleteDefinitionDetailById(Long definitionInfo)
    {
        return definitionDetailMapper.deleteDefinitionDetailById(definitionInfo);
    }

     @Override
     public void deploy(Long definitionId, String filePath) throws FileNotFoundException {
          //1 把文件部署到Activiti中,生成DeployMent对象
          Deployment deployment = processService.deploy(filePath);

          //封装DefinitionDetail对象,把数据存储到数据库
          DefinitionDetail detail = new DefinitionDetail();
          //封装DefinitionInfo对象,把传入的id设置对象中
          DefinitionInfo info = new DefinitionInfo();
          info.setId(definitionId);
          detail.setDefinitionInfo(info);//流程定义对象
          detail.setDeploymentId(deployment.getId());//关联Activiti中的部署ID
          definitionDetailMapper.insertDefinitionDetail(detail);






    }
}
